package com.ly.aicode.core;

import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.StrUtil;
import com.ly.aicode.ai.model.HtmlCodeResult;
import com.ly.aicode.ai.model.MultiFileCodeResult;
import com.ly.aicode.model.enums.CodeGenTypeEnum;

import java.io.File;
import java.nio.charset.StandardCharsets;

/**
 * @author luyi
 * @date 2025/11/7 23:55
 */
@Deprecated
public class CodeFileSaver {
    private static final String BASE_PATH = System.getProperty("user.dir")+"/temp/code_output";

    //写入文件
    private static void writeToFile(String content, String fileName,String dir) {
        String path = dir + File.separator + fileName;
        FileUtil.writeString(content,path, StandardCharsets.UTF_8);
    }

    //构造唯一目录路径
    private static String buildUniqueDir(String bizType) {
        String fileName = StrUtil.format("{}_{}", bizType, IdUtil.getSnowflakeNextIdStr());
        String dirPath= BASE_PATH + File.separator + fileName;
        FileUtil.mkdir(dirPath);
        return dirPath;
    }

    /**
     * 保存 HtmlCodeResult
     */
    public static File saveHtmlCodeResult(HtmlCodeResult result) {
        String baseDirPath = buildUniqueDir(CodeGenTypeEnum.HTML.getValue());
        writeToFile(baseDirPath, "index.html", result.getHtmlCode());
        return new File(baseDirPath);
    }

    /**
     * 保存 MultiFileCodeResult
     */
    public static File saveMultiFileCodeResult(MultiFileCodeResult result) {
        String baseDirPath = buildUniqueDir(CodeGenTypeEnum.MULTI_FILE.getValue());
        writeToFile(baseDirPath, "index.html", result.getHtmlCode());
        writeToFile(baseDirPath, "style.css", result.getCssCode());
        writeToFile(baseDirPath, "script.js", result.getJsCode());
        return new File(baseDirPath);
    }

}
